<?php

namespace App\Containers\AppSection\Data\Actions;

use App\Containers\AppSection\Data\Models\Company;
use App\Containers\AppSection\Data\Tasks\UpdateCompanyTask;
use App\Containers\AppSection\Data\UI\API\Requests\UpdateCompanyRequest;
use App\Containers\AppSection\Log\Tasks\CreateLogTask;
use App\Containers\AppSection\Profile\Tasks\UpdateProfileTask;
use App\Ship\Parents\Actions\Action;

class UpdateCompanyAction extends Action
{
    public function run(UpdateCompanyRequest $request): Company
    {
        $data = $request->sanitizeInput([
            'name',
            'ogrn',
            'inn',
            'legal_address',
            'actual_address',
            'address_matches',
            'charters',
        ]);

        //Обновление данных компании
        $company = app(UpdateCompanyTask::class)->run($request->profile()->legal_form->company->id, $data);

        //Смена статуса аккредитации
        if (!$request->profile()->hasAccreditationStatus('accreditation_returned'))
            app(UpdateProfileTask::class)
                ->run($request->profile()->id, [
                    'accreditation_status_key' => 'accreditation_not_accredited',
                    'accredited_at' => null
                ]);


        app(CreateLogTask::class)->run($request->profile()->id, 'Редактирование данных компании', 'profile');

        return $company;
    }
}
